<?php
/**
 * 功能: 通知管理
 * User: WD
 * Date: 2018/9/5 0005
 * Time: 15:45
 */

namespace app\admin\controller;
use app\base\controller\Power;
use think\Db;

class Notice extends Power{
    protected $beforeActionList = [
        'first'=>['except'=>'index,notice_add']

    ];
    protected function first(){
        $result = $this->testing();
    }
    /**
     * 通知列表
     */
    public function index(){
        $notice = Db::name('notice')->order('id desc')->paginate(10);
        $count  = Db::name('notice')->count();
        $this->assign([
            'notice'=>$notice,
            'count'=>$count,
            'empty'=>'<tr class="text-c empty"><td colspan="7">暂无数据</td></tr>'
        ]);
        return view('');

    }
    /**
     * 添加
     */
    public function notice_add(){
        $id = intval(input('id'));
        if($id){
            $result = Db::name('notice')->where('id',$id)->find();
            $this->assign('result',$result);
        }
        return $this->fetch('notice:notice-add');
    }

    public function notice_hold(){
        $content = trim(input('content'));
        if(empty($content)){
            return json(['msg'=>'请填写完整的数据','ok'=>false]);
        }
        $check = Db::name('notice')->where('content',$content)->find();
        if($check){
            return json(['msg'=>'该内容已经存在','ok'=>false]);
        }
        $res = Db::name('notice')->insert(['content'=>$content,'time'=>time()]);
        if($res){
            return json(['msg'=>'添加成功','ok'=>true]);
        }else{
            return json(['msg'=>'添加失败','ok'=>false]);
        }
    }

    public function notice_edit(){
        $id    = intval(input('notice_id'));
        $content = trim(input('content'));
        if(empty($content)){
            return json(['msg'=>'请填写完整的数据','ok'=>false]);
        }
        $check = Db::name('notice')->whereNotIn('id',$id)->where('content',$content)->find();
        if($check){
            return json(['msg'=>'该内容已经存在','ok'=>false]);
        }
        $res = Db::name('notice')->where('id',$id)->update(['content'=>$content,'time'=>time()]);
        if($res){
            return json(['msg'=>'修改成功','ok'=>true]);
        }else{
            return json(['msg'=>'修改失败','ok'=>false]);
        }
    }
    /**
     * 删除
     */
    public function notice_del(){
        $id    = intval(input('notice_id'));
        if($id<=0){
            return json(['msg'=>'id必须为正整数','ok'=>false]);
        }
        $res = Db::name('notice')->where('id',$id)->delete();
        if($res){
            return json(['msg'=>'删除成功','ok'=>true]);
        }else{
            return json(['msg'=>'删除失败','ok'=>false]);
        }
    }

}